package grailst
import winterwell.jtwitter.OAuthSignpostClient
import winterwell.jtwitter.Twitter
import winterwell.jtwitter.Twitter.Status

class LoginController {
	static String consumerKey = "94l4la4Ews1sFTDwWxCTKQ"
	static String consumerSecret = "jfM506MvlpzYEqalaQmUjdsFOECk6E3OfaGDVTgAw"

    def index = {
		
			redirect(action: "login", params: params)
		
		
	}
	
	def login = {
		if(session.usuario == null){
		String callBack = createLink(controller: 'login', action: 'validando')
			OAuthSignpostClient oath = new OAuthSignpostClient(consumerKey, consumerSecret, "http://localhost:8080/grailsT/login/validando")
			
			URI url = oath.authorizeUrl()
			
			flash.pagina = url.toString()
			
			session.autenticado = oath
		}
		else{
			redirect(controller:'principal',action:'index')
		}	
	}
	
	def validando = {
	//oauth_verifier
		String pin = params.oauth_verifier
		String nome = params.nome
		
		OAuthSignpostClient oath = session.autenticado
		
		
		oath.setAuthorizationCode(pin)
		
		Object accessToken = oath.getAccessToken()
		
		Twitter usuario = new Twitter("",oath)
		
		session.usuario = usuario
		
		
		redirect(controller:'principal',action:'index')
		
	}
}
